package com.lfx.concurrent;

import java.util.Random;

public class WriterThread extends Thread{

    private final  Data data;


    private Random random=new Random();

    private final String filler;

    private int index = 0;

    public WriterThread(Data data, String filler) {
        this.data = data;
        this.filler = filler;
    }


    @Override
    public void run() {
        while (true){
            try {
                char c= nextChar();
                data.writer(c);
                Thread.sleep(random.nextInt(3000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private char nextChar() {
        char c = filler.charAt(index);
        index++;
        if(index>=filler.length())
            index=0;
        return c;
    }


}
